﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnLineExam.Web.mag.Handler
{
    /// <summary>
    /// HttpPostedFileHandler 的摘要说明
    /// </summary>
    [System.Web.Services.WebService(Namespace = "http://tempuri.org/")]
    [System.Web.Services.WebServiceBinding(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1)]
    public class HttpPostedFileHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";
            HttpPostedFile file = context.Request.Files["Filedata"];
            string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
            try
            {
                if (file != null)
                {
                    if (!System.IO.Directory.Exists(uploadPath))
                    {
                        System.IO.Directory.CreateDirectory(uploadPath);
                    }
                    file.SaveAs(uploadPath + file.FileName);
                    string fileType = System.IO.Path.GetExtension(file.FileName);//文件类型
                    if (fileType.ToLower() == ".doc" || fileType.ToLower() == ".xls" || fileType.ToLower() == ".ppt")
                    {
                        string newfilename = DateTime.Now.ToString("yyyyMMddhhmmss-") + file.FileName.Split('.')[0] + ".swf";

                        HHG.Common.MyConvert.ConvertToSWF(uploadPath + file.FileName, uploadPath + newfilename);
                        context.Response.Write(newfilename);
                    }
                    else
                    {
                        context.Response.Write(file.FileName);
                    }
                    //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失       
                   
                }
                else
                {
                    context.Response.Write("0");
                }
            }
            catch (Exception ex)
            {

                context.Response.Write("0");
            }
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}